package Launcher;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import flickR.FlickRConnector;
import Connectivity.DataBase;

public class Main2 {

	static int nb=0;
	
	public Main2()
	{
		
		
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		ArrayList alEmo = new ArrayList();
		DataBase db = new DataBase();

				// anger anticipation disgust fear joy sadness surprise trust
				int [] black = {1211,108,33,3170,4355,2436,903,1084};
				
				// anger anticipation disgust fear joy sadness surprise trust
				int [] blue = {845,217,27,2267,7538,2069,1419,2134};
				
				// anger anticipation disgust fear joy sadness surprise trust
				int [] brown = {398,51,17,527,1822,512,590,635};
				
				// anger anticipation disgust fear joy sadness surprise trust
				int [] green = {635,161,50,1502,6309,1219,1598,2386};
				
				// anger anticipation disgust fear joy sadness surprise trust
				int [] grey = {139,13,2,412,623,683,165,637};
				
				// anger anticipation disgust fear joy sadness surprise trust
				int [] orange = {351,76,14,925,3216,486,516,546};
				
				// anger anticipation disgust fear joy sadness surprise trust
				int [] pink = {249,137,13,659,2901,571,1321,793};
				
				// anger anticipation disgust fear joy sadness surprise trust
				int [] purple = {239,37,6,354,2389,277,418,244};
				
				// anger anticipation disgust fear joy sadness surprise trust
				int [] red = {1612,223,170,2521,2313,2024,1929,1814};
				
				// anger anticipation disgust fear joy sadness surprise trust
				int [] white = {729,288,27,1934,7596,1684,1553,1419};
				
				// anger anticipation disgust fear joy sadness surprise trust
				int [] yellow = {374,102,22,753,3159,679,881,732};

		alEmo.add("anger");
		alEmo.add("anticipation");
		alEmo.add("disgust");
		alEmo.add("fear");
		alEmo.add("joy");	
		alEmo.add("sadness");
		alEmo.add("surprise");
		alEmo.add("trust");

		ArrayList alColor = new ArrayList();
//		alColor.add("red");
//		alColor.add("blue");
//		alColor.add("brown");
//		alColor.add("black");
//		alColor.add("white");
//		alColor.add("pink");
//		alColor.add("yellow");
//		alColor.add("green");
//		alColor.add("orange");
//		alColor.add("grey");
//		alColor.add("purple");


		Connection co = db.connection();

		try{
			Statement st = co.createStatement();


			for(int i=0;i<alEmo.size();i++)
			{
				for(int j=0;j<alColor.size();j++)
				{
						
					buildHeaderTables(co, alColor.get(j).toString(), alEmo.get(i).toString(),purple[i]);
					//					createRecords(alColor.get(j).toString(), alEmo.get(i).toString());
					//					table="CREATE TABLE";
					//					String query="";
					//					query=table+" "+alColor.get(j)+"_"+alEmo.get(i)+"(idPhoto varchar(10), url varchar(100)" +
					//							", com varchar(50), date varchar(50), primary key(idPhoto))";
					//					System.out.println(query);
					//					st.executeUpdate(query);
					//					table="";
				}
			}

			co.close();


		}
		catch(SQLException s){
			System.out.println("Table already exists!");
			System.err.println("error : "+s.getErrorCode());
		}


	}




	private static void buildHeaderTables(Connection co, String color, String emotion, int red) {

		PreparedStatement prep = null;
		PrintWriter fichierHeader =null;

		File imgFile = new File("./headerT_POURCENTAGE/HeaderTable"+color+"_"+emotion+".txt");

		try {
			fichierHeader = new PrintWriter(imgFile);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}


		try {
			String currentTable = color+"_"+emotion;
			String query="SELECT T.idPhoto, valeur FROM tags T , "+currentTable+" P where " +
					"P.idPhoto = T.idPhoto";
			//			String query="SELECT T.idPhoto, valeur1 FROM tags T , "+currentTable+" P where " +
			//					"P.idPhoto = T.idPhoto and ";
			System.out.println(query);
			TreeMap <String, Integer> headerTable  = new TreeMap<String, Integer>();

			Set set = new TreeSet (   
					new Comparator ()   
					{   
						public int compare (Object o1, Object o2)
						{   
							Map.Entry e1 =  ( Map.Entry ) o1;
							Map.Entry e2 =  ( Map.Entry ) o2;



							Integer d1 =  ( Integer ) e1.getValue();
							Integer d2 =  ( Integer ) e2.getValue();

							//return d1.compareTo(d2);
							if(d1<=d2){
								return 1;
							}else{
								return -1;
							}
						}
					});

			System.out.println("corpus "+color+"_"+emotion);
			prep = co.prepareStatement(query);
			ResultSet res =  prep.executeQuery();
			while(res.next())
			{
				String id= res.getString(1);
				String valeur = res.getString(2);
				//nb++;
				if(valeur.contains("?")){
					continue;
				}
				if(headerTable.containsKey(valeur)){
					int cpt=headerTable.get(valeur);
					headerTable.remove(valeur);
					headerTable.put(valeur, cpt+1);
//					System.out.println("Tag: "+valeur+" ,occurrences: "+headerTable.get(valeur));
				}else{

					headerTable.put(valeur,1);
//					System.out.println("Tag2: "+valeur+" ,occurrences: "+headerTable.get(valeur));
				}
				//a d�commenter si tu veux checker ! 	
				//System.out.println(nb+" - id : "+id+ " - valeur : "+valeur);
			}

			System.out.println("Table non tri�e: \n"+headerTable.toString());
			//Pour avoir une map tri�e par fr�quence d�croissante
			set.addAll (headerTable.entrySet()) ;
			for  (  Iterator iter = set.iterator (  ) ; iter.hasNext (  ) ;  )   
			{   
				Map.Entry entry =  ( Map.Entry ) iter.next (  ) ;  
				
				
//				int x = 223;
//				int y = 55;
//				 double c = new Double(x);
//				double ratio = y / c;
//				
				
				int val = Integer.parseInt(entry.getValue().toString());
				
				
				 double conv = new Double(red);
				double ratio = val / conv;
//	
				fichierHeader.println((""+entry.getKey (  )  + " ; " + ratio )) ;
				//System.out.println(""+entry.getKey (  )  + " ; " + entry.getValue (  ) );
			}

			prep.close();
			fichierHeader.close();

		}
		catch (SQLException e) 
		{
			e.printStackTrace();
			System.err.println("SQLException: " + e.getMessage());

		}


	}

	private static void createRecords(String color, String emotion) {

		FlickRConnector fl = new FlickRConnector(color,emotion);
	}
}
